package com.zhangxi.websqlit.user.control;

import com.zhangxi.websqlit.common.control.BaseController;
import com.zhangxi.websqlit.mybatis.entity.PtContactEnity;
import com.zhangxi.websqlit.mybatis.entity.PtUserEntity;
import com.zhangxi.websqlit.user.entity.MailVfcCodeEntity;
import com.zhangxi.websqlit.user.entity.UserSearchEntity;
import com.zhangxi.websqlit.user.exception.MoreMailException;
import com.zhangxi.websqlit.user.exception.NotMailException;
import com.zhangxi.websqlit.user.service.UserInfoService;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserInfoController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private UserInfoService userInfoService;

    /**
     * 查询用户列表
     * @param search
     * @return
     */
    @RequestMapping("/queryUserList")
    @ResponseBody
    public List<PtUserEntity> queryUserList(UserSearchEntity search){
        logger.info(search);
        List<PtUserEntity> list = userInfoService.queryUserList(search);
        return list;
    }

    /**
     * 登录
     * @param entity
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public String login(PtUserEntity entity){
        logger.info(entity);
        entity = userInfoService.queryLogin(entity.getUserName(),entity.getPassword());
        if(entity == null){
            return jsonFail("user.login.fail");
        }
        Map<String,Object> data = new HashMap<>();
        data.put("userUuid",entity.getUserUuid());
        return jsonDataSuccess("user.login.success",data);
    }

    /**
     * 根据用户ID查询用户信息
     * @param userUuid
     * @return
     */
    @RequestMapping("/queryUserInfoById")
    @ResponseBody
    public String queryUserInfoById(@RequestParam("userUuid") String userUuid){
        logger.info("userUuid:" + userUuid);
        PtUserEntity entity = userInfoService.queryUserInfoById(userUuid);
        return jsonDataSuccess("user.login.success",entity);
    }

    /**
     * 修改用户信息
     * @param entity
     * @param tipName
     * @return
     */
    @RequestMapping("/updateUserInfoById")
    @ResponseBody
    public String updateUserInfoById(PtUserEntity entity,@RequestParam("tipName") String tipName){
        logger.info(entity + " tipName:" + tipName);
        Integer count = userInfoService.updateUserInfoById(entity);
        if(count == 0){
            return jsonSuccess("user.update.success",tipName);
        }else{
            return jsonFail("user.update.exit",tipName);
        }
    }

    /**
     * 修改用户密码
     * @param entity
     * @return
     */
    @RequestMapping("/updateUserPasswdById")
    @ResponseBody
    public String updateUserPasswdById(PtUserEntity entity){
        logger.info(entity );
        userInfoService.updateUserPasswdById(entity);
        return jsonSuccess("user.update.passwd.success");
    }

    /**
     * 用户注册
     * @param entity
     * @return
     */
    @RequestMapping("/regiterUser")
    @ResponseBody
    public String regiterUser(PtUserEntity entity){
        logger.info(entity );
        userInfoService.regiterUser(entity);
        return jsonSuccess("user.register.success");
    }

    /**
     * 查询相应用户信息数量
     * @param entity
     * @param tipName
     * @return
     */
    @RequestMapping("/queryUserInfoCount")
    @ResponseBody
    public String queryUserInfoCount(PtUserEntity entity,@RequestParam("tipName") String tipName){
        logger.info(entity + " tipName:" + tipName);
        Integer count = userInfoService.queryUserInfoCount(entity);
        if(count == 0){
            return jsonCodeSuccess("user.query.no.exit",count,tipName);
        }else{
            return jsonCodeFail("user.query.exit",count,tipName);
        }
    }

    /**
     * 上传用户头像
     * @param request
     * @return
     */
    @RequestMapping("/unloadPhoto")
    @ResponseBody
    public String unloadPhoto(MultipartHttpServletRequest request) throws IOException {
        MultipartFile file = request.getFile("file");
        String fileName = "";
        if(file == null){
            return jsonFail("user.unload.photo.fail");
        }
        fileName = file.getOriginalFilename();
        logger.info("fileName:" + fileName );
        String userUuid = request.getParameter("userUuid");
        logger.info("userUuid:" + userUuid );
        String fileUuid = userInfoService.unloadPhoto(fileName,file.getInputStream(),userUuid);
        return jsonUUIDSuccess("user.unload.photo.success",fileUuid);
    }

    /**
     * 查询好朋列表
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/queryFriendsListById")
    @ResponseBody
    public String queryFriendsListById(@RequestParam("userUuid") String userUuid,HttpServletRequest request) throws IOException {
        String searchUserKey = request.getParameter("searchUserKey");
        logger.info("userUuid:" + userUuid + " searchUserKey:" + searchUserKey );
        List<PtContactEnity> list = userInfoService.queryFriendsListById(userUuid,searchUserKey);
        return jsonDataSuccess("user.query.friends.success",list);
    }

    /**
     * 获取验证码
     * @return
     */
    @RequestMapping("/getMailVfcCode")
    @ResponseBody
    public String getMailVfcCode(@RequestParam("email") String email){
        logger.info("email:" + email);
        String verificationCode = null;
        try {
            verificationCode = userInfoService.getMailVfcCode(email);
        } catch (NotMailException e) {
            e.printStackTrace();
            return jsonFail("mail.not.exit.fail");
        } catch (MoreMailException e) {
            e.printStackTrace();
            return jsonFail("mail.exit.more.fail");
        }
        if(verificationCode == null || "".equals(verificationCode)){
            return jsonFail("mail.verification.code.fail");
        }
        return jsonDataSuccess("mail.verification.code.success",verificationCode);
    }

    /**
     * 根据邮件，修改用户密码
     * @param entity
     * @return
     */
    @RequestMapping("/updatePasswordByMail")
    @ResponseBody
    public String updatePasswordByMail(MailVfcCodeEntity entity){
        logger.info(entity);
        if(entity.getEmail() == null || entity.getPassword() == null){
            return jsonFail("mail.update.password.param.fail");
        }

        try {
            userInfoService.updatePasswordByMail(entity);
        } catch (NotMailException e) {
            e.printStackTrace();
            return jsonFail("mail.not.exit.fail");
        } catch (MoreMailException e) {
            e.printStackTrace();
            return jsonFail("mail.exit.more.fail");
        }
        return jsonSuccess("mail.update.password.success");

    }


}
